﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using GenericLibrariesToolKit;
using BLL.Users;
using BLL.Users.Impl;
namespace BLL.Privilege.Impl
{
    public class RoleRightBOImpl:DaoSupport<RoleRight>,RoleRightBO
    {
        public  bool IsPrivilege(string userId, string modelId, OperateType operateType)
        {
            
            //获取用户类型
            User user = GetRoleId(userId);
            if (user == null)
                return false;
            //获取模块权限和
            int modelRightsum;
            try
            {
                modelRightsum = RoleModelRightImpl.roleModelRight[user.UserType][modelId];
            }
            catch (Exception ex)
            { return false; }
            Console.WriteLine(modelRightsum.ToString());
            //判断用户是否对该模块有指定操作的权限
            switch (operateType)
            {
                case OperateType.add:
                    if (modelRightsum % 2 == 1)
                        return true;
                    break;
                case OperateType.update:
                    if (modelRightsum % 4 >= 2 && modelRightsum % 4 <= 3)
                        return true;
                    break;
                case OperateType.delete:
                    if (modelRightsum % 8 >= 4 && modelRightsum % 8 <= 7)
                        return true;
                    break;
                case OperateType.view:
                    if (modelRightsum >= 8 && modelRightsum <= 15)
                        return true;
                    break;
                default:break;
            }
            return false;
        }
        /// <summary>
        /// 得到用户的角色类型
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public User GetRoleId(string userId)
        {
            //得到用户角色类型
            UserBO userBO = new UserBOImpl();
            User user = userBO.Find(userId);
            return user;
        }
    }
}
